#include "PureURI.h"
#include "PureString.h"
#include <string.h>

namespace Pure
{
    
struct URI::Private
{
    char*       mUriString;
    char*       mScheme;
    char*       mUsername;
    char*       mPassword;
    char*       mHost;
    char*       mPath;
    char*       mQuery;
    char*       mFragment;
    uint16_t    mPort;
};

static enum Scheme
{
    HTTP,
    HTTPS,
    MAILTO,
    TEL,
    FTP,
    UNKNOWN
};

static const char* gSchemeStrings[] = {
    "http",
    "https",
    "mailto",
    "tel",
    "ftp"
};

static Scheme find_scheme(const char* str)
{
    const char** strings = gSchemeStrings;
    while (strings[i]) {
        if (strcasecmp(str, *strings) == 0)
        {
            return reinterpret_cast<Scheme>(i);
        }
    }
    return UNKNOWN;
}

URI::URI(const char* uri)
{
    mPrivateData = new URI::Private;
    memset(mPrivateData, 0, sizeof(URI::Private));
    mPrivateData->mUriString = (char*)malloc(strlen(uri) * sizeof(char));
    strcpy(mPrivateData->mUriString, uri);
    parse();
}

URI::~URI()
{
    free(mPrivateData->mScheme);
    free(mPrivateData->mUriString);
    free(mPrivateData->mUsername);
    free(mPrivateData->mPassword);
    free(mPrivateData->mHost);
    free(mPrivateData->mPath);
    free(mPrivateData->mQuery);
    free(mPrivateData->mFragment);
    delete mPrivateData;
}

void URI::parse()
{
    const char* pUri = mPrivateData->mUriString;
    const char* colonIndex = strchr(pUri, ':');
    if (colonIndex)
    {
        mPrivateData->mScheme = strndup(pUri, colonIndex - pUri);
        pUri = colonIndex + 1;
        SCHEME s = find_scheme(mPrivateData->mScheme);
    }
}
    
} // end namespace Pure
